// src/main/java/com/ibuu/demos/service/impl/LoginServiceImpl.java
package com.ibuu.demos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ibuu.common.CustomException;
import com.ibuu.demos.mapper.LoginMapper;
import com.ibuu.demos.mapper.UserMapper;
import com.ibuu.demos.pojo.Num;
import com.ibuu.demos.pojo.User;
import com.ibuu.demos.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, User> implements LoginService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public boolean login(String username, String password) {
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getUsername, username);
        User one = getOne(qw);
        if (one == null) {
            throw new CustomException("用户不存在");
        }
        if (!one.getPassword().equals(password) )
        {
            System.err.println("密码错误");
            return false;
        }
        return true;
    }
    
    @Override
    public User selectUser(String username) {
        User user = userMapper.selectUser(username);
        return user;
    }

    @Override
    public boolean updateUser(User user) {
        int i = userMapper.updateById(user);
        if (i == 0) {
            return false;
        }
        return true;
    }

    @Override
    public boolean register(User user) {
        if (user.getUsername()==null){
            return false;
        }
        if (user.getPassword()==null){
            return false;
        }
        if (userMapper.selectUser(user.getUsername())!=null){
            return false;
        }
        user.setId(UUID.randomUUID().toString());

        userMapper.insert(user);


        return true;
    }

}